package com.yanqu.road.dao.impl.activity.fivehero;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fivehero.cross.CrossFiveHeroData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFiveHeroDataDaoImpl extends TempDataObjectDao<CrossFiveHeroData> {

    public static String getCrossFiveHeroDataSql = "select * from `t_c_activity_fivehero_data` where `activityId`=? AND groupId = ?";

    public CrossFiveHeroData getCrossFiveHeroData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFiveHeroDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossFiveHeroDataSql = "update `t_c_activity_fivehero_data` set " +
            "lockUnion=?,lockPatrons=?,bookMakerMap=?,winnerMap=?" +
            " where activityId=? AND groupId=?";

    @Override
    protected boolean updateData(CrossFiveHeroData crossFiveHeroData) {
        crossFiveHeroData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, crossFiveHeroData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossFiveHeroData.isLockPatrons()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroData.getBookMakerMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroData.getWinnerMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroData.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossFiveHeroDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFiveHeroDataSql = "insert into `t_c_activity_fivehero_data`" +
            "(groupId,activityId,lockUnion,lockPatrons,bookMakerMap,winnerMap)" +
            " values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFiveHeroData crossFiveHeroData) {
        crossFiveHeroData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFiveHeroData.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossFiveHeroData.isLockUnion()));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossFiveHeroData.isLockPatrons()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroData.getBookMakerMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFiveHeroData.getWinnerMap())));
        if (getDb().executeNoneQuery(insertCrossFiveHeroDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFiveHeroData parseData(ResultSet rs) throws SQLException {
        CrossFiveHeroData crossFiveHeroData = new CrossFiveHeroData();
        crossFiveHeroData.setActivityId(rs.getInt("activityId"));
        crossFiveHeroData.setGroupId(rs.getInt("groupId"));
        crossFiveHeroData.setLockUnion(rs.getBoolean("lockUnion"));
        crossFiveHeroData.setLockPatrons(rs.getBoolean("lockPatrons"));
        crossFiveHeroData.setBookMakerMap(JSON.parseObject(rs.getString("bookMakerMap"), new TypeReference<ConcurrentHashMap<String, Map<Long, Integer>>>() {
        }));
        crossFiveHeroData.setWinnerMap(JSON.parseObject(rs.getString("winnerMap"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {
        }));
        return crossFiveHeroData;
    }

}
